import os
import sys 
from flask import Flask


def create_app(test_config=None):
    # create and configure the app
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_pyfile('settings.py', silent=True)

    # ensure the instance folder exists
    try:
        os.makedirs(app.instance_path)
    except OSError:
        pass
    
    # 每次请求完毕之后断开数据库连接
    from db import close_db
    app.teardown_appcontext(close_db)

    # 注册蓝图
    from . import user
    app.register_blueprint(user.bp)

    return app